/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iodegree.circuitTable;

import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 *
 * @author Libra
 */
public class CircuitTableFrame extends javax.swing.JFrame {

    /**
     * Creates new form Frame
     */
    public CircuitTableFrame() {
        initComponents();
    }

    public void init() {
        canvas.setExp(exp[0]);
        canvas.setObs(obs[0]);
        canvas.setProb(prob[0]);
        canvas.setBoolDetails(chkDetails.isSelected());
        canvas.setDashed(chkDash.isSelected());
        canvas.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnNext = new javax.swing.JButton();
        btnPrev = new javax.swing.JButton();
        txtN = new javax.swing.JTextField();
        btnGoto = new javax.swing.JButton();
        chkFile = new javax.swing.JCheckBox();
        canvas = new iodegree.circuitTable.PatternCanvas();
        chkDetails = new javax.swing.JCheckBox();
        chkDash = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        btnPrev.setText("Prev");
        btnPrev.setEnabled(false);
        btnPrev.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrevActionPerformed(evt);
            }
        });

        txtN.setText("0");
        txtN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNActionPerformed(evt);
            }
        });

        btnGoto.setText("Goto");
        btnGoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGotoActionPerformed(evt);
            }
        });

        chkFile.setText("Save File");

        canvas.setPreferredSize(new java.awt.Dimension(320, 300));

        javax.swing.GroupLayout canvasLayout = new javax.swing.GroupLayout(canvas);
        canvas.setLayout(canvasLayout);
        canvasLayout.setHorizontalGroup(
            canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 320, Short.MAX_VALUE)
        );
        canvasLayout.setVerticalGroup(
            canvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        chkDetails.setText("Details");

        chkDash.setText("Dashed");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(canvas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnPrev)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtN, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnGoto)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnNext))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(chkDetails)
                                .addGap(18, 18, 18)
                                .addComponent(chkDash)
                                .addGap(18, 18, 18)
                                .addComponent(chkFile, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrev)
                    .addComponent(txtN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGoto)
                    .addComponent(btnNext))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chkFile)
                    .addComponent(chkDetails)
                    .addComponent(chkDash))
                .addGap(18, 18, 18)
                .addComponent(canvas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        if (counter < 69) {
            counter++;
            txtN.setText(Integer.toString(counter));
            draw(counter);
            btnPrev.setEnabled(true);
        } else {
            btnNext.setEnabled(false);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnPrevActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrevActionPerformed
        if (counter > 0) {
            counter--;
            txtN.setText(Integer.toString(counter));
            draw(counter);
            btnNext.setEnabled(true);
        } else {
            btnPrev.setEnabled(false);
        }
    }//GEN-LAST:event_btnPrevActionPerformed

    private void btnGotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGotoActionPerformed
        int temp = Integer.parseInt(txtN.getText());
        if (temp >= 0 && temp <= 69) {
            counter = temp;
//            setPara();
            draw(counter);
            if (counter == 0) {
                btnPrev.setEnabled(false);
            } else if (counter == 69) {
                btnNext.setEnabled(false);
            }
        }
    }//GEN-LAST:event_btnGotoActionPerformed

    private void txtNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNActionPerformed

    private void draw(int n) {

        canvas.setObs(obs[n]);
        canvas.setExp(exp[n]);
        canvas.setProb(prob[n]);
        canvas.setId(n);
        canvas.setBoolDetails(chkDetails.isSelected());
        canvas.setDashed(chkDash.isSelected());
        canvas.repaint();

        if (chkFile.isSelected()) {
//            D.tp("tried");
            BufferedImage bi = ScreenImage.createImage(canvas);
            String prefix = counter < 10 ? "0" : "";
            try {
                ScreenImage.writeImage(bi, "../img_out/image" + prefix + Integer.toString(counter) + ".png");
            } catch (IOException e) {
                System.err.println(e.toString());
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CircuitTableFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CircuitTableFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGoto;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnPrev;
    private iodegree.circuitTable.PatternCanvas canvas;
    private javax.swing.JCheckBox chkDash;
    private javax.swing.JCheckBox chkDetails;
    private javax.swing.JCheckBox chkFile;
    private javax.swing.JTextField txtN;
    // End of variables declaration//GEN-END:variables
    private int counter = 0;
    int[] obs;
    double[] exp;
    double[] prob;

    public void setObs(int[] obs) {
        this.obs = obs;
    }

    public void setExp(double[] exp) {
        this.exp = exp;
    }

    public void setProb(double[] prob) {
//        this.prob = prob;
        double[] fixedMulti=new double[prob.length];
        for(int i=0;i<prob.length;i++){
            fixedMulti[i]=prob[i]*9;
        }
        this.prob = fixedMulti;
    }
}
